package com.admin.server.util;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;


import com.admin.server.config.PMF;
import com.admin.server.domain.Menu;
import com.admin.server.domain.Privilegio;
import com.admin.server.domain.Rol;
import com.admin.server.domain.Usuario;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class LoadData {
	public static void load(){
		//loadPrivilegio();//1
		//loadMenu();//2
		//loadRol();//3
		setUserRol();//4
	}
	
	
	private static void loadPrivilegio(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		List<Privilegio> list = new ArrayList<Privilegio>();
		
		try{			
			Privilegio pr = new Privilegio("Super Usuario");
			Usuario user = new Usuario("admin", "123", "admin", "admin");
			pr.addUsuario(user);
			list.add(pr);
			list.add(new Privilegio("Usuario"));
			pm.makePersistentAll(list);			
		}finally{
			pm.close();
		}
		
	}
	private static void loadMenu(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			Menu root = new Menu();
			root.setMenu("Root");
			root.setParametro("root");
			root.setHaschildren(true);
			root.setTipo(1);
			
			Menu parent = new Menu();
			parent.setMenu("Seguridad");
			parent.setParametro("");
			parent.setHaschildren(true);
			parent.setTipo(1);
			
			Menu child = new Menu();
			child.setMenu("Menus");
			child.setParametro("menus");
			child.setHaschildren(false);
			child.setTipo(1);
			
			parent.addChildren(child);
			
			root.addChildren(parent);
			
			pm.makePersistent(root);
		}finally{
			pm.close();
		}
	}

	private static void loadRol(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = null;
		try{
			Rol root = new Rol();
			root.setDescripcion("Root");
			root.setHaschildren(true);
			root.setHasusers(false);
			
			Rol child = new Rol();
			child.setDescripcion("Administradores");
			child.setHaschildren(false);
			child.setHasusers(true);
			
			root.addChildren(child);
			tx = pm.currentTransaction();
			tx.begin();
			pm.makePersistent(root);
			tx.commit();
			
		}finally{			
			if(tx.isActive()){
				tx.rollback();
			}
			pm.close();
		}
	}
	
	private static void setUserRol(){
		KeyFactory.Builder keyBuilder =	new KeyFactory.Builder(Privilegio.class.getSimpleName(), 1);
		keyBuilder.addChild(Usuario.class.getSimpleName(), 3);
		Key ckey = keyBuilder.getKey();//Usuario administrador
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Usuario e = pm.getObjectById(Usuario.class, ckey);
		
		KeyFactory.Builder keyBuilder2 = new KeyFactory.Builder(Rol.class.getSimpleName(), 7);
		keyBuilder2.addChild(Rol.class.getSimpleName(), 8);
		Key ckey2 = keyBuilder2.getKey();//Rol administradores
		
		e.addRol_id(ckey2);
		pm.close();
		
		PersistenceManager pm2 = PMF.get().getPersistenceManager();
		Rol rol = pm2.getObjectById(Rol.class, ckey2);
		
		rol.addUsuario_id(ckey);
		pm2.close();
	}
	
	public static void read() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Usuario.class);
		try{
			
			List<Usuario> results = (List<Usuario>) query.execute();	
			String id = null;
	        if (!results.isEmpty()) {
	            for (Usuario e : results) {
	            	System.out.println(e.getKey());
	            	id = KeyFactory.keyToString(e.getKey());
	            	System.out.println(id);
	            }
	        }
			//Key k = KeyFactory.createKey(Usuario.class.getSimpleName(), "1");
			//Privilegio(1)/Usuario(3)
			//KeyFactory.Builder keyBuilder =	new KeyFactory.Builder(Privilegio.class.getSimpleName(), 1);
			//keyBuilder.addChild(Usuario.class.getSimpleName(), 3);
			//Key ckey = keyBuilder.getKey();
			
	        Key ckey = KeyFactory.stringToKey(id);
			Usuario e = pm.getObjectById(Usuario.class, ckey);
			System.out.println(e.getPrivilegio().getDescripcion());
		}finally{
			pm.close();
			query.closeAll();
		}
	}
	public static void readMenu() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		//Query query = pm.newQuery(Menu.class);
		try{
			//query.setFilter("parametro == :pa");
			
//			List<Menu> results = (List<Menu>) query.execute("");
//			if (!results.isEmpty()) {
//	            for (Menu e : results) {
//	            	System.out.println(e.getKey());
//	            	System.out.println(e.getChildren().size());
//	            }
//	        }
			Key k = KeyFactory.createKey(Menu.class.getSimpleName(), 4);//root
			Menu e = pm.getObjectById(Menu.class, k);
			//System.out.println(e.getMenu());
			//System.out.println(e.getChildren().size());
			for (Menu m : e.getChildren()) {
            	System.out.println(m.getKey());
            	System.out.println(m.getMenu());
            	System.out.println(m.getChildren().size());
            	
            }
		}finally{
			pm.close();
			//query.closeAll();
		}
	}
}
